package com.xmut.mall.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.xmut.mall.store.annotation.CheckJwt;
import com.xmut.mall.store.searchparam.LogisticsSearchParam;
import com.xmut.mall.store.utils.RespBean;
import com.xmut.mall.store.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.store.entity.LogisticsEntity;
import com.xmut.mall.store.service.LogisticsService;
import com.xmut.mall.store.utils.PageUtils;
import com.xmut.mall.store.utils.R;



/**
 * 
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2023-02-13 03:45:06
 */
@RestController
@RequestMapping("store/logistics")
public class LogisticsController {
    @Autowired
    private LogisticsService logisticsService;

    /**
     * 根据id获取物流公司信息
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getLogisticsById/{id}")
    public RespBean getLogisticsById(@PathVariable Long id){
        return RespBean.success("获取成功",logisticsService.getById(id));
    }

    /**
     * 获取商家已选择物流公司列表
     * @param storeId
     * @return
     */
    @CheckJwt
    @GetMapping("/getChecked/{storeId}")
    public RespBean getChecked(@PathVariable Long storeId){
        return logisticsService.getChecked(storeId);
    }

    /**
     * 获取所有的物流公司
     * @return
     */
    @CheckJwt
    @GetMapping("/getAllLogistics")
    public RespBean getAllLogistics(){
        return logisticsService.getAllLogistics();
    }

    /**
     * 修改物流公司信息
     * @param entity
     * @return
     */
    @CheckJwt
    @PutMapping("/updateLogistics")
    public RespBean updateLogistics(@RequestBody LogisticsEntity entity){
        return logisticsService.updateLogistics(entity);
    }

    /**
     * 根据ID删除物流公司信息
     * @param id
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteLogistics/{id}")
    public RespBean deleteLogistics(@PathVariable Long id){
        return logisticsService.deleteLogistics(id);
    }

    /**
     * 添加物流公司
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/addLogistics")
    public RespBean addLogistics(@RequestBody LogisticsEntity entity){
        return logisticsService.addLogistics(entity);
    }

    /**
     * 官方管理平台获取物流公司分页数据
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerLogisticsByPage")
    public RespBean getManagerLogisticsByPage(@RequestBody LogisticsSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return logisticsService.getManagerLogisticsByPage(vo,param);
    }

    /**
     *获取商家物流公司列表，如果已选择则checked有值
     * @param storeId
     * @return
     */
    @CheckJwt
    @GetMapping("/getLogistics/{storeId}")
    public RespBean getLogistics(@PathVariable Long storeId){
        return logisticsService.getLogistics(storeId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = logisticsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		LogisticsEntity logistics = logisticsService.getById(id);

        return R.ok().put("logistics", logistics);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LogisticsEntity logistics){
		logisticsService.save(logistics);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LogisticsEntity logistics){
		logisticsService.updateById(logistics);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		logisticsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
